// components/popup/index.js
Component({
  options: {
    multipleSlots: true // 复数插槽: 是
  },
  /**
   * 组件的属性列表
   */
  properties: {
    visible: {
      type: Boolean,
      value: false
    },
    title: {
      type: String,
      value: ''
    },
    content: {
      type: String,
      value: ''
    },
    showCancel: {
      type: Boolean,
      value: true
    },
    cancelText: {
      type: String,
      value: '取消'
    },
    cancelColor: {
      type: String,
      value: '#666666'
    },
    confirmText: {
      type: String,
      value: '确定'
    },
    confirmColor: {
      type: String,
      value: '#fca400'
    },
    editable: {
      type: Boolean,
      value: false
    },
    placeholderText: {
      type: String,
      value: ''
    },
    details: {
      type: Object,
      value: {}
    },
    mode: {
      type: String,
      value: 'modal'
    },
    list: {
      type: Array,
      value: []
    }
  },

  /**
   * 组件的初始数据
   */
  observers: {
    'list': function (list) {
      if(list.length>0){
        this.setData({
          couponId:list[0].id
        })
      }
    }
  },
  data: {
    text: '',
  },

  /**
   * 组件的方法列表
   */
  methods: {
    confirm() {
      const {
        text,
        editable,
        placeholderText,
        couponId
      } = this.data
      if (editable) {
        if (text.length == 0) {
          wx.showToast({
            title: placeholderText,
            icon: 'none'
          })
          return
        }
        if (!this.validatePhone(text)) {
          return
        }
      }
      this.setData({
        visible: false
      })
      this.triggerEvent('confirm', editable ? text : couponId)
    },
    cancel() {
      this.setData({
        visible: false
      })
      this.triggerEvent('cancel')
    },
    validatePhone(phone) {
      var flag = false
      const isValidPhone = /^[1]([3-9])[0-9]{9}$/.test(phone);
      if (isValidPhone) {
        flag = true
      } else {
        flag = false

        wx.showToast({
          icon: 'none',
          title: '手机号不正确',
        });
      }
      return flag;

    },
    onText(e) {
      this.setData({
        text: e.detail.value
      })
    },
    // 点击一键呼叫
    haddleCall() {
      let _this = this
      const {
        details
      } = this.data
      const phoneList = details.phone.split(/\n/g)
      getApp().call(phoneList)
    },
    copy(e) {
      const text = e.currentTarget.dataset.text
      getApp().copy(text)
    },
    receive(e) {
      const {
        id
      } = e.currentTarget.dataset
      this.setData({
        couponId: id
      })
    },
  }
})